package com.jackstraw.tools.utils;

import org.reflections.Reflections;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 所在包：com.wenhe.test
 * 文件名：
 * 项目：test
 * 功能描述：
 * 修改人：Xlz
 * 修改时间：2020-10-09 16:08
 */
public class ControllerPathUtil {

    /*<!-- 反射框架 -->
    <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections</artifactId>
        <version>0.9.11</version>
    </dependency>*/

    public static void main(String[] args) {
        Reflections reflections = new Reflections("com.wenhe.test.controller");
        Set<Class<?>> classSet = reflections.getTypesAnnotatedWith(RestController.class);
        for (Class clz : classSet) {
            System.out.println(clz.getName());
            getPath(clz);
        }
    }

    public static void getPath(Class clz) {
        List<String> pathList = new ArrayList<>();
        String classPath = "";
        RequestMapping clzReq = (RequestMapping) clz.getAnnotation(RequestMapping.class);
        if (clzReq != null) {
            String[] value = clzReq.value();
            if (value.length > 0)
                classPath = value[0];
        }
        Method[] methods = clz.getDeclaredMethods();
        for (Method method : methods) {
            RequestMapping req = method.getAnnotation(RequestMapping.class);
            if (req != null) {
                String[] reqPath = req.value();
                listAddArr(classPath, pathList, reqPath);
            } else {
                GetMapping get = method.getAnnotation(GetMapping.class);
                if (get != null) {
                    String[] getPath = get.value();
                    listAddArr(classPath, pathList, getPath);
                } else {
                    PostMapping post = method.getAnnotation(PostMapping.class);
                    if (post != null) {
                        String[] postPath = post.value();
                        listAddArr(classPath, pathList, postPath);
                    } else {
                        PutMapping put = method.getAnnotation(PutMapping.class);
                        if (put != null) {
                            String[] putPath = put.value();
                            listAddArr(classPath, pathList, putPath);
                        } else {
                            DeleteMapping delete = method.getAnnotation(DeleteMapping.class);
                            if (delete != null) {
                                String[] deletePath = delete.value();
                                listAddArr(classPath, pathList, deletePath);
                            }
                        }
                    }
                }
            }
        }
        System.out.println(pathList);
    }

    public static void listAddArr(String classPath, List<String> list, String ...path) {
        if (!classPath.startsWith("/")) {
            classPath = "/" + classPath;
        }
        for (String p : path) {
            if (!classPath.endsWith("/") && !p.startsWith("/"))
                list.add(classPath + "/" + p);
            else
                list.add(classPath + p);
        }
    }
}
